/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package opener;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import myarrays.MyArrays;

/**
 *
 * @author asus
 */
public class OpenDat extends GeneralOpener {

    private double[][] DATA;
    private double[] timedelay;
    private double[] wavelength;

    public OpenDat(String filepath) {
        super(filepath);
        getwholedata();
    }

    /**
     * @return fata as double array of doubles
     */
    @Override
    public double[][] getdata() {
        return DATA;
    }

    /**
     * @return timedelay as array of doubles
     */
    @Override
    public double[] gettimedelay() {
        return timedelay;
    }

    /**
     * @return wavelength as array of doubles
     */
    @Override
    public double[] getwavelength() {
        return wavelength;
    }

    private void getwholedata() {
        ArrayList<double[]> ar_data = new ArrayList<double[]>();
        String strRead;
        try {
            FileReader fr = new FileReader(this.filepath);
            BufferedReader brs = new BufferedReader(fr);
            while ((strRead = brs.readLine()) != null) {
                String[] splitarray = strRead.split("\t");
                double[] a = new double[splitarray.length];

                for (int i = 0; i < splitarray.length; i++) {
                    a[i] = Double.parseDouble(splitarray[i]);
                }
                ar_data.add(a);
            }
            fr.close();
        } catch (IOException ex) {
            ex.printStackTrace(System.out);
        }
        double[][] var = new double[ar_data.size()][ar_data.get(0).length];
        for (int i = 0; i < var.length; i++) {
            System.arraycopy(ar_data.get(i), 0, var[i], 0, var[0].length);
        }
        timedelay = new double[var[0].length - 1];
        timedelay = Arrays.copyOfRange(var[0], 1, var[0].length);
        wavelength = new double[var.length - 1];
        wavelength = MyArrays.takecolumn(MyArrays.deleterow(var, 0), 0);
        DATA = MyArrays.deletecolumn(MyArrays.deleterow(var, 0), 0);
    }
}
